<template>
	<view>
		<view class="container">

			<u-form labelPosition="left" labelWidth="80" :model="form" :rules="rules" ref="uForm">

				<u-form-item @click="pickerShow = true" required label="影视分类" prop="filmCategoryId" borderBottom>
					<u-input v-model="form.filmCategoryName" disabled disabledColor="#ffffff" placeholder="请选择影视分类"
						border="none"></u-input>
					<u-icon slot="right" name="arrow-right"></u-icon>
					<u-picker :show="pickerShow" title="影视分类" ref="uPicker" :columns="filmCategoryList" keyName="name"
						@cancel="cancelHandler" @confirm="confirmHandler">
					</u-picker>
				</u-form-item>

				<u-form-item required label="影视名称" prop="name" borderBottom>
					<u-input v-model="form.name" placeholder="请输入影视名称" border="none"></u-input>
				</u-form-item>

				<u-form-item required label="封面图" prop="coverImg" borderBottom>

					<u-upload ref="uUpload" uploadText="最大支持2MB" :fileList="coverImgFiles" :afterRead="afterRead"
						@delete="deletePic" :beforeRead="beforeRead" name="coverImgUpload" :maxCount="1" :maxSize="2">
					</u-upload>


				</u-form-item>

				<u-form-item required label="简介" prop="introduce" borderBottom>
					<u-textarea maxlength="200" v-model="form.introduce" placeholder="请输入内容"></u-textarea>
				</u-form-item>
			</u-form>

		</view>

	</view>
</template>

<script>
	import http from "@/utils/http.js"
	export default {
		data() {
			return {
				flag: false,
				id: '',
				pickerShow: false,
				form: {
					filmCategoryId: null,
					name: null,
					introduce: null,
					coverImg: null,
					filmCategoryName: null
				},
				fromData: {},
				coverImgFiles: [],
				rules: {
					filmCategoryId: {
						type: 'number',
						required: true,
						message: '请选择影视分类',
						trigger: ['blur', 'change']
					},
					name: {
						type: 'string',
						required: true,
						message: '请填写影视名称',
						trigger: ['blur', 'change']
					},
					coverImg: {
						type: 'string',
						required: true,
						message: '请上传封面图',
						trigger: ['blur', 'change']
					},
					introduce: {
						type: 'string',
						required: true,
						message: '请添加简介',
						trigger: ['blur', 'change']
					},
				},
				filmCategoryList: [

				],
			}
		},
		onLoad(options) {
			if (options.filmId) {
				uni.setNavigationBarTitle({
					title: '编辑影视'
				})
				this.flag = true
				this.id = options.filmId
			} else {
				uni.setNavigationBarTitle({
					title: '添加影视'
				})
				this.flag = false
			}
			if (this.flag) {
				this.upfilmData()
			}
			this.getfilmCategory()

		},

		methods: {
			confirmHandler(e) {
				this.pickerShow = false
				const {
					columnIndex,
					indexs,
					// 微信小程序无法将picker实例传出来，只能通过ref操作
					picker = this.$refs.uPicker
				} = e
				this.form.filmCategoryName = e.value[0].name
				this.form.filmCategoryId = e.value[0].id
			},

			cancelHandler(e) {
				this.pickerShow = false
			},
			//获取分类
			getfilmCategory() {
				var params = {
					url: `/film/filmCategory/list`,
					method: "GET",
					callBack: (res) => {
						if (res.code == 200) {
							this.filmCategoryList = [res.data]
						}
					}
				};
				http.request(params);
			},
			//获取电影详情
			async upfilmData() {
				var params = {
					url: `/film/filmShort/u/getById/${this.id}`,
					method: "GET",
					callBack: (res) => {
						if (res.code == 200) {
							this.from = res.data
							this.from.filmCategoryName = null
						}
					}
				};
				await http.request(params);
			},
			//图片
			deletePic(event) {
				this.coverImgFiles.splice(event.index, 1)
			},
			afterRead(file) {
				console.log(22)
				console.log(file)

				let lists = [].concat(file.file)
				let fileListLen = this[`coverImgFiles`].length
				lists.map((item) => {
					this[`coverImgFiles`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})

				for (let i = 0; i < lists.length; i++) {
					const result = this.uploadFilePromise(lists[i].url)
					let item = this[`coverImgFiles`][fileListLen]
					this[`coverImgFiles`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				console.log(url)
			},

			beforeRead(file, lists, name) {
				//2097152   25224
				if (file.size >= 2097152) {
					uni.showToast({
						title: '大小不得超过2MB',
						icon: 'error'
					})
					return
				}
				this.$refs.uUpload.afterRead(file)
			}
		}
	}
</script>

<style lang="scss">
	.container {
		margin-left: 30rpx;
		margin-right: 30rpx;
	}
</style>
